package com.css.bjdt_core.report.service;

import java.io.File;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_core.cell.entity.Cell;
import com.css.bjdt_core.executive.report.ReportCache;
import com.css.bjdt_core.report.entity.RptAreaTemporary;
import com.css.bjdt_core.report.entity.RptGroup;
import com.css.bjdt_core.report.entity.RptTemplate;
import com.css.bjdt_core.report.entity.RptTemplateCalc;
import com.css.bjdt_core.report.entity.RptTemplateCloneInfo;
import com.css.bjdt_core.report.entity.RptTemplateCondition;
import com.css.bjdt_core.report.entity.RptTemplateStatCategory;
import com.css.bjdt_core.report.entity.RptTemplateValidation;
import com.css.bjdt_core.reportPrepare.entity.ViewRptGroupTemplate;
import com.css.bjdt_core.util.CommonUtil;
import com.css.bjdt_core.util.PageQuerys;

@Service
public class ReportTemplateManageService {

	@Autowired
	private Dao dao;

	@Autowired
	private ReportGroupManageService reportGroupManageService;

	private ReportCache cache = ReportCache.getInstance();

	private final static String CACHE_NAME = ReportTemplateManageService.class.getSimpleName();

	//保存子报表信息
	public void saveTemplate(RptTemplate rptTemplate) {
		if (StringUtils.isEmpty(rptTemplate.getTemplateId())) {
			rptTemplate.setTemplateId(CommonUtil.getUUID());
		}
		Date date = new Date(new java.util.Date().getTime());
		rptTemplate.setCreateDate(date);
		dao.insert(rptTemplate);
	}

	//得到一条子报表相信信息
	public RptTemplate getAnRawRptTemplate(String templateId) {
		RptTemplate rptTemplate = dao.fetch(RptTemplate.class, Cnd.where("TEMPLATEID", "=", templateId));
		//根据子报表查出来套表名称
		RptGroup rptGroup = reportGroupManageService.queryByID(rptTemplate.getGroupId());
		rptTemplate.setGroupName(rptGroup.getName());
		return rptTemplate;
	}

	//得到一条子报表相信信息
	public RptTemplate getAnRptTemplate(String templateId) throws NullPointerException {
		RptTemplate rptTemplate = (RptTemplate) cache.get(CACHE_NAME, templateId);
		if (rptTemplate == null) {
			//先得到子报表
			rptTemplate = dao.fetch(RptTemplate.class, Cnd.where("TEMPLATEID", "=", templateId));
			//根据子报表查出来套表名称
			RptGroup rptGroup = reportGroupManageService.queryByID(rptTemplate.getGroupId());
			rptTemplate.setGroupName(rptGroup.getName());

			cache.put(CACHE_NAME, templateId, rptTemplate);
		}
		return rptTemplate;
	}

	//获得当前年度的所有子报表
	public List<ViewRptGroupTemplate> getRptTemplateByAnnualId(String annualid) {
		if (StringUtils.isBlank(annualid)) {
			return null;
		}
		return dao.query(ViewRptGroupTemplate.class,
				Cnd.where("ANNUALID", "=", annualid).asc("ORDERID").asc("TEMPLATEORDERID"));
	}

	public List<ViewRptGroupTemplate> getRptTemplateByIds(String... templateIds) {
		List<ViewRptGroupTemplate> viewRptGroupTemplateList = dao.query(ViewRptGroupTemplate.class,
				Cnd.where("TEMPLATEID", "IN", templateIds));
		for (ViewRptGroupTemplate viewRptGroupTemplate : viewRptGroupTemplateList) {
			viewRptGroupTemplate.setPid(viewRptGroupTemplate.getGroupId());
			viewRptGroupTemplate.setIsGroupRpt("1");
		}
		return viewRptGroupTemplateList;
	}

	public List<ViewRptGroupTemplate> getRptTemplateByIdsLussed(String... templateIds) {
		List<ViewRptGroupTemplate> viewRptGroupTemplateList = dao.query(ViewRptGroupTemplate.class,
				Cnd.where("TEMPLATEID", "IN", templateIds).asc("TEMPLATEORDERID"));
		List<String> groupIdList = new ArrayList<String>();
		for (ViewRptGroupTemplate viewRptGroupTemplate : viewRptGroupTemplateList) {
			viewRptGroupTemplate.setPid(viewRptGroupTemplate.getGroupId());
			viewRptGroupTemplate.setIsGroupRpt("1");
			if (groupIdList.contains(viewRptGroupTemplate.getGroupId())) {
				continue;
			} else {
				groupIdList.add(viewRptGroupTemplate.getGroupId());
			}
		}
		for (String groupId : groupIdList) {
			RptGroup rptGroup = reportGroupManageService.queryByID(groupId);
			ViewRptGroupTemplate viewRptGroupTemplate = new ViewRptGroupTemplate();
			viewRptGroupTemplate.setAnnualId(rptGroup.getAnnualId());
			viewRptGroupTemplate.setDescription(rptGroup.getDescription());
			viewRptGroupTemplate.setGroupId(rptGroup.getGroupId());
			viewRptGroupTemplate.setName(rptGroup.getName());
			viewRptGroupTemplate.setOrderId(rptGroup.getOrderId());
			viewRptGroupTemplate.setPid("0");
			viewRptGroupTemplate.setTemplateId(rptGroup.getGroupId());
			viewRptGroupTemplate.setTemplateName(rptGroup.getName());
			viewRptGroupTemplate.setIsGroupRpt("0");
			viewRptGroupTemplateList.add(viewRptGroupTemplate);
		}
		return viewRptGroupTemplateList;
	}

	//删除一条子报表信息
	public void deleteAnRptTemplate(String templateId, String annualId) {
		//先删报表模板
		String url = CommonUtil.readProperties();
		String fileName = templateId + ".cll";
		File cellFile = new File(url + "/" + annualId + "/", fileName);
		if (cellFile.isFile() && cellFile.exists()) {
			cellFile.delete();
		}
		//删除子报表
		dao.clear(RptTemplate.class, Cnd.where("TEMPLATEID", "=", templateId));
		//删除报表中的区域
		dao.clear(RptAreaTemporary.class, Cnd.where("TEMPLATEID", "=", templateId));
		//删除报表的统计类别
		dao.clear(RptTemplateStatCategory.class, Cnd.where("TEMPLATE_ID", "=", templateId));
		//删除报表的总控条件、行条件、列条件
		dao.clear(RptTemplateCondition.class, Cnd.where("TEMPLATE_ID", "=", templateId));
		//删除报表的行列计算
		dao.clear(RptTemplateCalc.class, Cnd.where("TEMPLATE_ID", "=", templateId));
		//删除报表的校核公式
		dao.clear(RptTemplateValidation.class, Cnd.where("TEMPLATE_ID", "=", templateId));
		//删除报表的单元格
		dao.clear(Cell.class, Cnd.where("TEMPLATE_ID", "=", templateId));
		//删除这张报表的克隆信息
		dao.clear(RptTemplateCloneInfo.class,
				Cnd.where("TEMPLATE_NEW_ID", "=", templateId).and("ANNUAL_NEW_ID", "=", annualId));

	}

	//更新一条子报表
	public void updateAnRptTemplate(RptTemplate rptTemplate) {
		Date date = new Date(new java.util.Date().getTime());
		rptTemplate.setUpdateDate(date);
		dao.updateIgnoreNull(rptTemplate);
	}

	//子报表查询
	public Map queryPage(Condition cnd, int pageNum, int pageSize) {
		Map result = PageQuerys.ligerGridQuery(dao, RptTemplate.class, cnd, pageNum, pageSize);
		/*List<RptTemplate> rptTemplateList = (List<RptTemplate>) result.get("Rows");
		result.put("Rows", rptTemplateList);*/
		return result;
	}

	//通过套表ID获得该套表下的所有子报表
	public List<RptTemplate> getAllRptTemplateByGroupId(String groupId) {
		return dao.query(RptTemplate.class, Cnd.where("GROUPID", "=", groupId));
	}

	//通过子表的ID，获得套表
	public ViewRptGroupTemplate getAViewRptGroupTemplate(String templateId) {
		return dao.fetch(ViewRptGroupTemplate.class, Cnd.where("TEMPLATEID", "=", templateId));
	}

	public ViewRptGroupTemplate getAViewRptGroupTemplateByName(String templateName, String annualId) {
		return dao.fetch(ViewRptGroupTemplate.class, Cnd.where("TEMPLATENAME", "like", "%" + templateName + "%")
				.and("ANNUALID", "=", annualId).orderBy("TEMPLATENAME", "ASC"));
	}

	public RptTemplate getAReportInfo(String templateId) {
		return dao.fetch(RptTemplate.class, Cnd.where("TEMPLATEID", "=", templateId));
	}
}
